﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuaInterface;
using UnityEngine;
using UnityEngine.EventSystems;

namespace FrameWork.Plugins.HollowJ.common
{
    public class EventTriggerListenner : MonoBehaviour, IPointerClickHandler, IPointerUpHandler, IPointerDownHandler,
        IPointerEnterHandler, IPointerExitHandler
    {
        static Dictionary<GameObject, EventTriggerListenner> eventListenners = new Dictionary<GameObject, EventTriggerListenner>();
        public LuaFunction OnPointerClickLua;
        public LuaFunction OnPointerUpLua;
        public LuaFunction OnPointerDownLua;
        public LuaFunction OnPointerEnterLua;
        public LuaFunction OnPointerExitLua;

        public static EventTriggerListenner GetEventListenner(GameObject gameObject)
        {
            EventTriggerListenner eventTriggerListenner;
            if (eventListenners.ContainsKey(gameObject))
            {
                eventTriggerListenner = eventListenners[gameObject];
            }
            else
            {
                eventTriggerListenner = gameObject.AddComponent<EventTriggerListenner>();
                eventListenners.Add(gameObject, eventTriggerListenner);
            }
            return eventTriggerListenner;
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            if (OnPointerClickLua != null)
            {
                OnPointerClickLua.Call(gameObject, eventData);
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (OnPointerUpLua != null)
            {
                OnPointerUpLua.Call(gameObject, eventData);
            }
        }


        public void OnPointerDown(PointerEventData eventData)
        {
            if (OnPointerDownLua != null)
            {
                OnPointerDownLua.Call(gameObject, eventData);
            }
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (OnPointerEnterLua != null)
            {
                OnPointerEnterLua.Call(gameObject, eventData);
            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (OnPointerExitLua != null)
            {
                OnPointerExitLua.Call(gameObject, eventData);
            }
        }
    }
}